package com.itmk.web.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmk.web.stu_role.entity.StuRole;
import com.itmk.web.stu_role.service.StuRoleService;
import com.itmk.web.student.entity.StuParm;
import com.itmk.web.student.entity.Student;
import com.itmk.web.student.mapper.StudentMapper;
import com.itmk.web.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author java实战基地
 * @Version 2383404558
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private StuRoleService stuRoleService;

    @Override
    public void addStu(Student student) {
        int insert = this.baseMapper.insert(student);
        if (insert > 0) {
            //设置学生的角色
            StuRole stuRole = new StuRole();
            stuRole.setStuId(student.getStuId());
            stuRole.setRoleId(student.getRoleId());
            stuRoleService.save(stuRole);
        }
    }

    @Override
    public void editStu(Student student) {

        int i = this.baseMapper.updateById(student);
        if (i > 0) {
            //先删除原来的角色
            QueryWrapper<StuRole> query = new QueryWrapper<>();
            query.lambda().eq(StuRole::getStuId, student.getStuId());
            stuRoleService.remove(query);
            //设置学生的角色
            StuRole stuRole = new StuRole();
            stuRole.setStuId(student.getStuId());
            stuRole.setRoleId(student.getRoleId());
            stuRoleService.save(stuRole);
        }

    }

    @Override
    public void deleteStu(Long stuId) {

        int i = this.baseMapper.deleteById(stuId);
        if (i > 0) {
            //先删除原来的角色
            QueryWrapper<StuRole> query = new QueryWrapper<>();
            query.lambda().eq(StuRole::getStuId, stuId);
            stuRoleService.remove(query);
        }
    }

    @Override
    public IPage<Student> getList(StuParm parm) {
        IPage<Student> page = new Page<>(parm.getCurrentPage(), parm.getPageSize());
        return this.baseMapper.getList(page, parm);
    }
}
